using System;
using System.Net;
using System.IO;

namespace c3o.Framework.Web
{
    public class SiteReader
    {
        private CookieContainer cookies = new CookieContainer();
                
        public SiteReader()
        {            
        }

        /// <summary>
        /// Post data to a page
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="PostString"></param>
        public void PostPage(string Url, string PostString)
        {
            WebHelper.PostPage(Url, PostString, ref cookies);

            //HttpWebRequest webRequest = WebRequest.Create(Url) as HttpWebRequest;

            //// Read the response
            //StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
            //string responseData = responseReader.ReadToEnd();
            //responseReader.Close();

            //// extract the viewstate value and build out POST data            

            //// have a cookie container ready to receive the forms auth cookie
            //this.cookies = new CookieContainer();

            //// Now post to the login form
            //webRequest = WebRequest.Create(Url) as HttpWebRequest;
            //webRequest.Method = "POST";
            //webRequest.ContentType = "application/x-www-form-urlencoded";
            //webRequest.CookieContainer = this.cookies;

            //// write the form values into the request message
            //StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
            //requestWriter.Write(PostString);
            //requestWriter.Close();
                        
            //// we don't need the contents of the response, just the cookie it issues
            //webRequest.GetResponse().Close();            
        }



        /// <summary>
        /// Return results for page hit
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string ReadPage(string url)
        {
            return WebHelper.ReadPage(url, ref cookies);

            //// Setup the request
            //HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
            //wr.CookieContainer = this.cookies;

            //StreamReader sr = new StreamReader(wr.GetResponse().GetResponseStream());

            //// Read to the end
            //string response = sr.ReadToEnd();
            //sr.Close();

            //return response;
        }



        /// <summary>
        /// Return binary array of file
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public byte[] ReadFile(string url)
        {
            return WebHelper.ReadFile(url, ref cookies);

            //byte[] result;
            //byte[] buffer = new byte[4096];

            //HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;

            //wr.CookieContainer = this.cookies;

            //using (WebResponse response = wr.GetResponse())
            //{
            //    using (Stream responseStream = response.GetResponseStream())
            //    {
            //        using (MemoryStream memoryStream = new MemoryStream())
            //        {
            //            int count = 0;
            //            do
            //            {
            //                count = responseStream.Read(buffer, 0, buffer.Length);
            //                memoryStream.Write(buffer, 0, count);

            //            } while (count != 0);

            //            result = memoryStream.ToArray();

            //        }
            //    }
            //}

            //return result;
        }


        /// <summary>
        /// Download and save file to disk
        /// </summary>
        /// <param name="url"></param>
        /// <param name="filename"></param>
        public void Download(string url, string filename)
        {
			WebHelper.Download(url, filename, ref cookies);

            //byte[] FileData = this.ReadFile(url);

            //// Create file
            //FileStream fs = File.Create(filename);

            ////Write File
            //BinaryWriter bw = new BinaryWriter(fs);

            //bw.Write(FileData);

            //// Close up
            //bw.Close();
            //fs.Close();
        }
    }
}
